package no.ntnu.idi.deid.decision.validators;

import java.util.List;

import no.ntnu.idi.deid.documentunits.Token;
import no.ntnu.idi.deid.searchresultunits.CategoryTag;
import no.ntnu.idi.deid.searchresultunits.Tag;

public class PhoneFaxNumberValidator {
	
	public static boolean isValidPhoneOrFaxNumber(List<Token> list) {
		String phoneString = "";
		for(Token token: list) {
			phoneString += token.getStripped();
		}
		phoneString = phoneString.replaceAll(" |[^0-9]", "");
		switch (phoneString.length()) {
		case 12:
			if(phoneString.charAt(0) != '0' ||
			phoneString.charAt(1) != '0' || 
			phoneString.charAt(2) != '4' || 
			phoneString.charAt(3) != '7') {
				return false;
			}
			phoneString = phoneString.substring(4);
			break;
		case 10:
			if(phoneString.charAt(0) != '4' || phoneString.charAt(1) != '7') {
				return false;
			}
			phoneString = phoneString.substring(2);
			break;			
		default:
			break;
		}
		if(phoneString.length() == 8) {
			try {
				Integer.parseInt(phoneString);
				return true;
			} catch (NumberFormatException e) {
				return false;
			}		
		}
		else {
			return false;
		}
	}

	public static void checkPhoneOrFax(List<Token> list) {
		if(isValidPhoneOrFaxNumber(list)) {
			for(Token tok: list) {
				tok.addTag(Tag.TLFNUMBER);
			}
		}
	}
	

}
